package com.wimcorp.magic.data.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import com.wimcorp.magic.data.AbilityData;
import com.wimcorp.magic.data.DeckData;

@Transactional
public class AbilityDataDaoImpl implements AbilityDataDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public long saveOrUpdate(AbilityData o) {
		if (o.getId() == null) {
			entityManager.persist(o);
			return o.getId();
		} else {
			return entityManager.merge(o).getId();
		}
	}

	@Override
	public AbilityData getAbility(String ability) {
		List<AbilityData> results = new ArrayList<>();
		try {
			results = entityManager
					.createQuery("select a from AbilityData a where ability=:ability", AbilityData.class)
					.setParameter("ability", ability).getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (results.size() > 0) {
			return results.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void delete(AbilityData ability) {
		entityManager.remove(ability);
		
	}

}
